package org.UserHouse.Servlet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.UserHouse.Dao.AddressDao;
import org.UserHouse.Dao.Imp.AddressDaoImp;
import org.UserHouse.Dao.Imp.OrderDaoImp;
import org.UserHouse.Dao.OrderDao;
import org.UserHouse.entity.Address;
import org.UserHouse.entity.Order;
import org.UserHouse.vo.PageVo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author bin
 * @date 2024-12-16
 */
@WebServlet("/list.Address.do")
public class listAddresssServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String address = req.getParameter("address");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        Address a = new Address();
        a.setAaddress(address);

        int intPage = 1;
        int intLimit = 5;

        if (page != null && page.matches("\\d+")) {
            intPage = Integer.parseInt(page);
        }

        if (limit != null && limit.matches("\\d+")) {
            intLimit = Integer.parseInt(limit);
        }

        PageVo pageVo = new PageVo();
        AddressDao addressDao = new AddressDaoImp();
        List<Address>addresslist = addressDao.selectPageAddress(a, intPage, intLimit);
        Long count = addressDao.selectPageCount(a);
        pageVo.setCount(count);
        pageVo.setData(addresslist);
        pageVo.setCode(0);


        String json = gson.toJson(pageVo);
        out.println(json);

        out.flush();
        out.close();
    }
}
